layui.use(['layer', 'table', 'form'], function () {
    let table = layui.table,
        form = layui.form,
        layer = layui.layer;
    layui.layer.load(0, {shade: [0.5, '#fff']});
    /*查询所有公告的信息*/
    table.render({
        elem: '#grade-table'
        , height: 420
        , toolbar: true
        , url: '/getGroupGrade'
        , where: {userName: USERNAME, queryKey: '', type: 2}
        , cols: [
            [ //标题栏
                {field: 'id', title: '编号', width: 90, align: 'center', sort: true, fixed: 'left'},
                {
                    field: 'paper', title: '论文标题', align: 'center', fixed: 'left', templet: function (res) {
                        return res.paper.title;
                    }
                },
                {field: 'sno', title: '学号', width: 140, align: 'center', fixed: 'left'},
                {
                    field: 'paper', title: '姓名', width: 100, align: 'center', templet: function (res) {
                        return res.paper.sname;
                    }
                },
                {
                    field: 'paper', title: '所属学院', width: 130, align: 'center', templet: function (res) {
                        return res.paper.title1.collegeName;
                    }
                },
                {
                    field: 'paper', title: '所属专业', width: 140, align: 'center', templet: function (res) {
                        return res.paper.title1.collegeName;
                    }
                },
                {
                    field: 'tname', title: '指导老师', width: 110, align: 'center', templet: function (res) {
                        return res.paper.title1.tName;
                    }
                },
                {
                    field: 'paper', title: '指导老师工号', width: 80, align: 'center', templet: function (res) {
                        return res.paper.title1.tno;
                    }
                },
                {
                    field: 'grade', title: '分数', width: 80, align: 'center', templet: function (res) {
                        if ((res.grade1 === 0 && res.grade2 === 0 && res.grade3 === 0) || (res.grade1 === '0' && res.grade2 === '0' && res.grade3 === '0')) {
                            return '<span style="color: #999999"> - - </span>'
                        } else {
                            let grade1 = parseInt(res.grade1);
                            let grade2 = parseInt(res.grade2);
                            let grade3 = parseInt(res.grade3);
                            let grade4 = parseInt(res.grade4);
                            let grade = grade3 + grade2 + grade1 + grade4;
                            if (grade < 60) {
                                return '<span style="color: red">' + grade + '</span>'
                            } else if (grade > 80) {
                                return '<span style="color: #009f95">' + grade + '</span>'
                            } else {
                                return '<span style="color: black">' + grade + '</span>'
                            }
                        }
                    }
                },
                {field: 'reply', title: '答辩次数', width: 80, align: 'center'},
                {
                    field: 'state', title: '评分状态', width: 100, align: 'center', templet: function (res) {
                        if (res.gradeState === null || res.gradeState === '') {
                            return '<span style="color: red">待评分</span>'
                        } else {
                            if (res.gradeState === '审核完成') {
                                return '<span style="color: #009688">审核完成</span>'
                            } else if (res.gradeState === '审核中') {
                                return '<span style="color: blue">审核中</span>'
                            } else {
                                return '<span style="color: red">待评分</span>'
                            }
                        }
                    }
                },
                {
                    field: 'passState', title: '审核状态', width: 100, align: 'center', templet: function (res) {
                        if (res.passState === null || res.passState === '') {
                            return '<span style="color: #999999">  - - </span>'
                        } else {
                            if (res.passState === '通过') {
                                return '<span style="color: #009688">通过</span>'
                            } else if (res.passState === '不通过') {
                                return '<span style="color: red">不通过</span>'
                            } else {
                                return '<span style="color: #999999">' + res.passState + '</span>'
                            }
                        }
                    }
                },
                {title: '课题详情', align: 'center', width: 120, toolbar: '#task-table-tool'},
                {title: '评分操作', align: 'center', width: 150, toolbar: '#assignment-table-tool', fixed: 'right'},
                {field: 'paper', title: '论文', width: 120, align: 'center', hide: true},
            ]
        ]

        // ,skin: 'line' //表格风格
        , even: true
        , page: true
        , limits: [6, 12, 18]
        , limit: 6
        , text: {
            none: '<div class="layui-anim layui-anim-scaleSpring"  style="margin-top: 50px"><i class="layui-icon"  style="color: #009f95;font-size: 150px;border-bottom: #009f95 solid 2px;padding:15px;margin-top: 50px" >&#xe664;</i><br>' +
                '<br><br><br><br><div class="layui-text" style="font-size: 25px;margin-top: 25px;color: #009f95">好像没有没找到数据呢</div></div>'
            ,
            error: '<div style="margin-top: 150px"><i class="layui-icon"  style="color: #009f95;font-size: 150px;border-bottom: #009f95 solid 2px;padding:15px;margin-top: 50px" >&#xe664;</i><br>' +
                '<br><div class="layui-text" style="font-size: 25px;margin-top: 25px;color: #009f95">好像没有出错了呢</div></div>'
        }
    });


    //监听表头事件  监听对象  allUSer
    table.on('toolbar(grade-table)', function (obj) {

        //查询事件
        if (obj.event === 'query') {  //按关键字搜索事件
            let queryKey = $('#queryKey').val().trim(); //获取搜索的关键字
            if (queryKey === null || queryKey === '') {
                layer.msg('请输入要查询内容', {offset: '250px', time: 1500, anim: 6, area: ['200px', '50px']});
                return false;
            }
            table.reload('grade-table', {
                where: {userName: USERNAME, queryKey: queryKey, type: 2}//设定异步数据接口的额外参数
                //,url: '/queryAll.indexDo' //重载接口地址 如果接口地址与原地址一样则不用写此项参数
            });

        } else if (obj.event === 'add') {  //查询全部
            table.reload('grade-table', {
                where: {userName: USERNAME, queryKey: '', type: 2}//设定异步数据接口的额外参数
                //,url: '/queryAll.indexDo' //重载接口地址 如果接口地址与原地址一样则不用写此项参数
            });
        }
    });


    /*监听操作栏事件*/
    table.on('tool(grade-table)', function (obj) {
        let data = obj.data;  //json格式的数据
        if (obj.event === 'taskDetail') {  //编辑事件
            layer.open({
                type: 1
                , offset: 50
                , skin: 'layui-layer-rim'
                , area: ['740px', '600px']
                , content: $("#task-detail")
                , title: '学生课题详情内容'
                , success: function (layerObj) {
                    $(".layui-layer-shade").appendTo(layerObj.parent());
                    /*设置回显数据*/
                    setTitle(data);
                }
                , end: function () {
                    $("#task-detail").css("display", 'none');
                }
            });
        } else if (obj.event === 'query-grade') {  //审核详情
            if (data.gradeState === '审核完成' || data.gradeState === '审核中') {
                layer.open({
                    type: 1
                    , offset: 0
                    , skin: 'layui-layer-rim'
                    , area: ['1040px', '800px']
                    , content: $("#teacher-grade--detail-box")
                    , title: '定稿评分详情'
                    , success: function (layerObj) {
                        $(".layui-layer-shade").appendTo(layerObj.parent());
                        setMsg(data);
                    }
                    , end: function () {
                        $("#teacher-grade--detail-box").css("display", 'none');
                    }
                });
            } else {
                layer.msg('此操作只有审核完定稿评分才可以操作', {anim: 6});
                return false;
            }

        } else if (obj.event === 'edit-grade') {  //评分
            if (data.gradeState === '审核完成' || data.gradeState === '审核中') {
                layer.msg('你已提交完成此论文的定稿评分', {anim: 6});
                return false;
            }
            layer.open({
                type: 1
                , offset: 0
                , skin: 'layui-layer-rim'
                , area: ['1040px', '800px']
                , content: $("#teacher-grade-box")
                , title: '中期检查'
                , success: function (layerObj) {
                    $(".layui-layer-shade").appendTo(layerObj.parent());
                    $("#sNO").val(data.paper.sno);
                    $("#sName").val(data.paper.sname);
                    $("#major").val(data.paper.title1.majorName);
                    $("#title").val(data.paper.title);
                    if (data.reply === null || data.reply ===''){
                        $("#reply1").val('一辩');//指导教师评
                    }else if (data.reply === '一辩'){
                        $("#reply1").val('二辩');//指导教师评
                    }
                }
                , end: function () {
                    $("#teacher-grade-box").css("display", 'none');
                }
            });
        }
    });


    /*提交成绩*/
    form.on('submit(add-grade)', function (data) {
        if (!checkGrade(data)) {
            return false;
        }
        let gradeData = JSON.stringify(data.field);
        let loadPage = layui.layer.load(0, {shade: [0.5, '#fff']});
        $.ajax({
            url: "/addGroupGrade",
            type: 'post',
            data: {
                gradeData: gradeData, userName: USERNAME
            },
            dataType: 'json',
            success: function (data) {  //调用接口成功
                if (data === true || data === 'true') {
                    layer.close(loadPage);
                    layer.closeAll('page');
                    layui.layer.msg('提交评分成功！', {offset: '300px', time: 2500, anim: 0});
                    //成功之后重新查询表格
                    table.reload('grade-table', {
                        where: {userName: USERNAME, queryKey: '', type: 2}//设定异步数据接口的额外参数
                    });
                } else {
                    layui.layer.close(loadPage);
                    layui.layer.alert('提交评分失败！');
                }
            },
            error: function () {  //接口错误
                layui.layer.close(loadPage);
                layui.layer.alert('服务器出错啦,请稍后再试！');
            }
        });
        return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
    });

    layui.layer.closeAll('loading');

});


function setMsg(data) {
    //接口查询
    //学生基本信息
    $("#sNO-detail").html(data.paper.sno);
    $("#sName-detail").html(data.paper.sname);
    $("#major-detail").html(data.paper.title1.majorName);
    $("#title-detail").html(data.paper.title);

    //论文基本信息
    if (data.reply === null || data.reply ===''){
        $("#reply").html(data.reply);//指导教师评

    }
    //各项分数
    let grade1 = parseInt(data.grade1);
    let grade2 = parseInt(data.grade2);
    let grade3 = parseInt(data.grade3);
    let grade4 = parseInt(data.grade4);
    let grade = grade1 + grade2 + grade3 + grade4;
    $("#detail-grade").html(grade);//总分
    $("#grade1").html(grade1);//详情分数1
    $("#grade2").html(grade2);//详情分数2
    $("#grade3").html(grade3);//详情分数3
    $("#grade4").html(grade4);//详情分数4
}


function checkGrade(data) {

    if (parseInt(data.field.grade1) < 0 || parseInt(data.field.grade1) > 35) {
        layer.msg('第 1 项评分不符合要求', {anim: 6});
        $("input[name = 'grade1']").focus();
        return false;
    }

    if (parseInt(data.field.grade2) < 0 || parseInt(data.field.grade2) > 15) {
        layer.msg('第 2 项评分不符合要求', {anim: 6});
        $("input[name = 'grade2']").focus();
        return false;
    }

    if (parseInt(data.field.grade3) < 0 || parseInt(data.field.grade3) > 35) {
        layer.msg('第 3 项评分不符合要求', {anim: 6});
        $("input[name = 'grade3']").focus();
        return false;
    }

    if (parseInt(data.field.grade4) < 0 || parseInt(data.field.grade4) > 15) {
        layer.msg('第 3 项评分不符合要求', {anim: 6});
        $("input[name = 'grade4']").focus();
        return false;
    }

    return true;
}





